package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.index.qual.PolyUpperBound;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManageController {

    //  调用服务层方法
    @Autowired
    private ManageService manageService;

    //  http://localhost/admin/product/1/10?category3Id=3
    //  @RequestBody json--->javaObject
    //  springmvc 对象传值
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
//                                 Long category3Id,
//                                 @RequestParam  Long category3Id,
//                                 HttpServletRequest request,
                                 SpuInfo spuInfo){
//        String c3Id = request.getParameter("category3Id");
        //  创建分页类！
        Page iPage = new Page(page,limit);
        //  调用服务层方法
        IPage spuInfoPage = manageService.getSpuInfoPage(iPage,spuInfo);
        //  返回数据
        return Result.ok(spuInfoPage);
    }

    //  /admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        //  调用服务层方法
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);

    }

    //  /admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //  调用服务层方法
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  /admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //  调用方法
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //  /admin/product/spuSaleAttrList/{spuId}
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        //  调用服务层方法
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);

        return Result.ok(spuSaleAttrList);

    }
}
